package com.xinshujia.utils;

import com.xinshujia.entity.QuartzEntity;
import org.quartz.*;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-12 11:38       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 *     定时任务工具类
 */
public class QuartzUtil {

    /**
     * 创建定时任务 定时任务创建之后默认启动状态
     * @param: scheduled 调度器
     * @param: quartzEntity 定时任务信息类
     */
    public static boolean createScheduleJob(Scheduler scheduler, QuartzEntity quartzEntity){
        boolean flag = false;
        try {
            //获取到定时任务的执行类  必须是类的绝对路径名称
            //定时任务类需要是job类的具体实现 QuartzJobBean是job的抽象类。
            Class<? extends Job> jobClass = (Class<? extends Job>) Class.forName(quartzEntity.getJobClass());
            // 构建定时任务信息
            JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(quartzEntity.getJobName()).build();
            // 设置定时任务执行方式
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzEntity.getCronExpression());
            // 构建触发器trigger
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(quartzEntity.getJobName()).withSchedule(scheduleBuilder).build();
            scheduler.scheduleJob(jobDetail, trigger);
            flag = true;
        } catch (ClassNotFoundException e) {
            System.out.println("定时任务类路径出错：请输入类的绝对路径");
        } catch (SchedulerException e) {
            System.out.println("创建定时任务出错：" + e.getMessage());
        }
        return flag;
    }

    /**
     * 根据任务名称暂停定时任务
     *
     * @param scheduler 调度器
     * @param jobName   定时任务名称
     */
    public static boolean pauseScheduleJob(Scheduler scheduler, String jobName) {
        boolean flag = false;
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.pauseJob(jobKey);
            flag = true;
        } catch (SchedulerException e) {
            System.out.println("暂停定时任务出错：" + e.getMessage());
        }
        return flag;
    }

    /**
     * 根据任务名称恢复定时任务
     *
     * @param scheduler 调度器
     * @param jobName   定时任务名称
     */
    public static boolean resumeScheduleJob(Scheduler scheduler, String jobName) {
        boolean flag = false;
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.resumeJob(jobKey);
            flag = true;
        } catch (SchedulerException e) {
            System.out.println("启动定时任务出错：" + e.getMessage());
        }
        return flag;
    }


    /**
     * 根据任务名称立即运行一次定时任务
     *
     * @param scheduler 调度器
     * @param jobName   定时任务名称
     */
    public static boolean runOnce(Scheduler scheduler, String jobName) {
        boolean flag = false;
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.triggerJob(jobKey);
            flag = true;
        } catch (SchedulerException e) {
            System.out.println("运行定时任务出错：" + e.getMessage());
        }
        return flag;
    }


    /**
     * 更新定时任务
     *
     * @param scheduler    调度器
     * @param quartzEntity 定时任务信息类
     */
    public static boolean updateScheduleJob(Scheduler scheduler, QuartzEntity quartzEntity) {
        boolean flag = false;
        try {
            //获取到对应任务的触发器
            TriggerKey triggerKey = TriggerKey.triggerKey(quartzEntity.getJobName());
            //设置定时任务执行方式
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzEntity.getCronExpression());
            //重新构建任务的触发器trigger
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
            //重置对应的job
            scheduler.rescheduleJob(triggerKey, trigger);
            flag = true;
        } catch (SchedulerException e) {
            System.out.println("更新定时任务出错：" + e.getMessage());
        }
        return flag;
    }


    /**
     * 根据定时任务名称从调度器当中删除定时任务
     *
     * @param scheduler 调度器
     * @param jobName   定时任务名称
     */
    public static boolean deleteScheduleJob(Scheduler scheduler, String jobName) {
        boolean flag = false;
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.deleteJob(jobKey);
            flag = true;
        } catch (SchedulerException e) {
            System.out.println("删除定时任务出错：" + e.getMessage());
        }
        return flag;
    }
}
